<?php

namespace app\api\controller\user;

use app\api\controller\Controller;

use app\api\model\Order as OrderModel;
use app\api\model\Setting as SettingModel;
use app\store\model\Order as StoreOrderModel;
use app\common\enum\OrderType as OrderTypeEnum;
use app\common\enum\order\PayType as PayTypeEnum;
use app\common\service\qrcode\Extract as ExtractQRcode;
use think\Db;

/**
 * 店员用户订单管理
 * Class Order
 * @package app\api\controller\user
 */
class Site extends Controller
{
    /* @var \app\api\model\User $user */
    private $user;
    private $shopId;

    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->user = $this->getUser();   // 用户信息

    }
    /**
     * 获取用户关联的店铺id
     * @param $key
     * @return mixed
     */
    protected function getUserShopId()
    {
       $shopInfo = Db::table('yoshop_store_shop_clerk')
            ->field('shop_id,clerk_id')
           ->where('status',1)
           ->where('is_delete',0)
//           ->where('user_id',10001)
           ->where('user_id',$this->user['user_id'])
           ->find();
        $shopId = $shopInfo?$shopInfo['shop_id']:0;
        $clerkId = $shopInfo?$shopInfo['clerk_id']:0;
        $shopInfo = [
            'shop_id'=>$shopId,
            'clerk_id'=>$clerkId
        ];
        return $shopInfo;
    }

    public function isShopSite(){
        $shopInfo = $this->getUserShopId();
        $shopId = $shopInfo['shop_id'];
        $clerkId = $shopInfo['clerk_id'];
        if(empty($shopId)||empty($clerkId)){
            return $this->renderError('管理站点信息有误');
        }
        return $this->renderSuccess(compact('shopId','clerkId'));

    }

    /**
     * 我的订单列表
     * @param $dataType
     * @return array
     * @throws \think\exception\DbException
     */
    public function lists($dataType)
    {
        $shopInfo = $this->getUserShopId();
        $shopId = $shopInfo['shop_id'];
        if(empty($shopId)){
            return $this->renderError('管理站点信息有误');
        }
        $model = new OrderModel;
        $list = $model->getSiteList($shopId, $dataType);
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 站点查看订单详情信息
     * @param $order_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function detail($order_id)
    {
        $shopInfo = $this->getUserShopId();
        $shopId = $shopInfo['shop_id'];
        if(empty($shopId)){
            return $this->renderError('管理站点信息有误');
        }
        // 订单详情
        $model = OrderModel::getSiteOrderDetail($order_id,$shopId);
        // 该订单是否允许申请售后
        $model['isAllowRefund'] = $model->isAllowRefund();
        return $this->renderSuccess([
            'order' => $model,  // 订单详情
            'setting' => [
                // 积分名称
                'points_name' => SettingModel::getPointsName(),
            ],
        ]);
    }



    /**
     * 站点  配送确认发货
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function delivery()
    {
        $order_id = $this->postData('order_id');

        $express_id= $this->postData('express_id');
        $express_no = $this->postData('express_no');
        $data = [
            'express_id'=>$express_id,
            'express_no'=>$express_no,
        ];

        $model = StoreOrderModel::detail($order_id);

        if ($model->delivery($data)) {
            return $this->renderSuccess('发货成功');
        }
        return $this->renderError($model->getError() ?: '发货失败');
    }


    /**
     * 站点  门店自提核销
     * @return array|bool
     * @throws \think\exception\DbException
     */
    public function extract()
    {
        $order_id = $this->postData('order_id');
        $extract_clerk_id= $this->postData('extract_clerk_id');
        $extract_status = $this->postData('extract_status');

        $data = [
            'extract_clerk_id'=>$extract_clerk_id,
            'extract_status'=>$extract_status,
        ];
//        $data = $this->postData('order');

        $model = StoreOrderModel::detail($order_id);

        $extract_clerk_id = (int)$extract_clerk_id[0];

        $wxapp_id = $this->postData('extract_wxapp_id');
        if ($model->verificationSiteOrder($model['order_id'],$extract_clerk_id,$model['shop_id'],$model['wxapp_id'])) {
            return $this->renderSuccess('核销成功');
        }
        return $this->renderError($model->getError() ?: '核销失败');
    }





}
